package view;

import game.environments.GameEnvironment;
import interfaces.IPaintable;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.util.Iterator;
import javax.swing.JComponent;


/**
 * Console class that contains a GameEnvironment.
 * @author Jenni Mercado
 * 
 */
@SuppressWarnings("serial")
public class Console extends JComponent {
    private static final int WIDTH = 600;
    private static final int HEIGHT = 700;

    private GameEnvironment myGameEnvironment;

    /** 
     * Constructor to set layout and size
     */
    public Console () {
        setLayout(new BorderLayout());
        // set component size
        setSize(WIDTH, HEIGHT);

        setFocusable(true);
        requestFocus();
    }

    /**
     * Calls inner paint method to paint all the objects of the 
     * gameenvironment to the component.
     * @param ge gameenvironment on the panel
     */
    public void paintLevel (GameEnvironment ge) {
        myGameEnvironment = ge;
        paint(getGraphics());
        repaint();
    }
    
    public void formatTextStyle(Graphics g){
         Font scoreFont = new Font("Arial", Font.BOLD, 20);
         g.setColor(Color.WHITE);
         g.setFont(scoreFont);
    }

    @Override
    public void paint (Graphics g) {
        Graphics2D pen = (Graphics2D) g;
        pen.drawImage(myGameEnvironment.getBackground(), 0, 0, this);
        formatTextStyle(pen);
        pen.drawString("Score: "+myGameEnvironment.getCurrentScore(), 100, 100);        
        Iterator<IPaintable> paintables = myGameEnvironment.getPaintables();
        while (paintables.hasNext()) {
            IPaintable paintable = paintables.next();
            pen.drawImage(paintable.getImage(), (int) paintable.getPosition().getX(),
                          (int) paintable.getPosition().getY(), this);

        }
    }

    /**
     * Takes an image and paints it to the console.
     * @param img image to be painted
     */
    public void paint(Image img) {
        Graphics2D pen = (Graphics2D) getGraphics();
        pen.drawImage(img, 0, 0, this);
    }
    
    public static int getConsoleWidth() {
        return WIDTH;
    }
    
    public static int getConsoleHeight() {
        return HEIGHT;
    }

}
